package com.sanctuary.dao.impls;

import java.sql.SQLException;
import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.sanctuary.dao.interfaces.PostDao;
import com.sanctuary.models.Post;

public class PostDaoImpl extends SqlMapClientDaoSupport implements PostDao {

	public int delete(Post p) throws SQLException {
		return getSqlMapClientTemplate().delete("delete-post",p);
	}

	public Post findById(int id) throws SQLException {
		Post p = new Post();
		p.setId(id);
		return (Post) getSqlMapClientTemplate().queryForObject("findById-post",p);
	}

	public List<Post> findByRatingThreshold(int threshold) throws SQLException {
		return (List<Post>) getSqlMapClientTemplate().queryForList("findByRatingThreshold-post",threshold);
	}

	public int save(Post p) throws SQLException {
		if (p.getId() == 0) {
			// new post, insert
			return ((Post) getSqlMapClientTemplate().insert("insert-post",p)).getId();
		} else {
			return getSqlMapClientTemplate().update("update-post",p);
		}
	}

}
